package com.panda.framework.netty.handler.extend;


import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import com.alibaba.fastjson.JSON;
import com.panda.framework.netty.common.Constant;
import com.panda.framework.netty.utils.MessageUtil;
import com.panda.system.common.constant.EnumConstants;
import com.panda.system.domain.SysNotice;
import com.panda.system.service.ISysNoticeService;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetNoticeListWebsocketHandler {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private ISysNoticeService service;
    /**
     * 保存任务明细
     * @author wechatno:tangjinjinwx
     * @param ctx
     * @param vo
     */
    @Async
    public  void handleMsg(ChannelHandlerContext ctx , TransportMessage vo, String contentJsonStr) {
        try {
            log.debug(contentJsonStr);
            SysNotice req =  JSON.parseObject(contentJsonStr,SysNotice.class);
            if(null != req){
                sendJson(ctx, service,req);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
        }
    }

    private static void  sendJson(ChannelHandlerContext ctx, ISysNoticeService service,SysNotice notice){
        notice.setStatus(EnumConstants.SysStatus.OPEN.getCode());
        List<SysNotice> sysNotices = service.selectNoticeList(notice);
        String json = null ;
        if(null != sysNotices && sysNotices.size()>0){
            json = JSON.toJSONString(sysNotices);
        }
        //3、告诉PC客户端消息已收到
        MessageUtil.sendCustomJsonMsg(ctx, "GetNoticeListResp", json);
    }


}
